﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CoolERP_Entities;
using CoolERP_BLL;
using CoolERP_Common;

namespace CoolERP_Web.User
{
    public partial class JCCD_EditCC_BZX : BasePage
    {
        private Guid DJID
        {
            get { return this.GetUrlParam("id") == null ? Guid.Empty : new Guid(this.GetUrlParam("id").ToString()); }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindFirst();
                BindData();
            }
        }

        private void BindFirst()
        {
            BindDepartment(this.ddlDepartments, EnumDepartment.Manufacturing, "请选择");
            BindJCCYY(this.ddlReason, "请选择");     
        }

        private void BindData()
        {
            if (DJID != Guid.Empty)
            {
                liTitle.Text = "修改";
                using (JCCDBLL jBLL = new JCCDBLL())
                {
                    Dictionary<string, string> dc = new Dictionary<string, string>();
                    dc.Add("ID", DJID.ToString());
                    int RecordCount = 0;
                    vJCCD entity = jBLL.GetJCCD(dc, "BH", null, 1, out RecordCount).FirstOrDefault();
                    txtGLDJ.Text = entity.GLDJID.Trim();
                    txtKHJC.Text = entity.KHJC;
                    hidKHID.Value = entity.GYSID.ToString();
                    txtBZ.Text = entity.BZ;
                    if (!string.IsNullOrEmpty(entity.JCCYYBH) && this.ddlReason.Items.FindByValue(entity.JCCYYBH) != null)
                        this.ddlReason.SelectedValue = entity.JCCYYBH;
                    if (entity.BMID != null && this.ddlDepartments.Items.FindByValue(entity.BMID.ToString()) != null)
                    {
                        this.ddlDepartments.SelectedValue = entity.BMID.ToString();
                        BindStaff(this.ddlDepartments.SelectedValue, this.ddlStaff, "请选择");
                        if (entity.RYID != null && this.ddlStaff.Items.FindByValue(entity.RYID.ToString()) != null)
                        {
                            this.ddlStaff.SelectedValue = entity.RYID.ToString();
                            hidStaff.Value = entity.RYID.ToString();
                        }
                    }
                    List<JCCDMXBModel> mxbModels = jBLL.GetJCCDMXB(entity.ID, 3);
                    repMXList.DataSource = mxbModels;
                    repMXList.DataBind();
                    foreach (var mxb in mxbModels)
                    {
                        hidWLID.Value += "," + mxb.WPID.ToString();
                        hidCounts.Value += "," + mxb.SL.ToString();
                        hidCKIDs.Value += "," + mxb.CKID.ToString();
                        hidCWIDs.Value += "," + mxb.CWID.ToString();
                        hidDWBH.Value += "," + mxb.DWBH;
                    }
                    hidWLID.Value = hidWLID.Value.Trim(',');
                    hidCounts.Value = hidCounts.Value.Trim(',');
                    hidCKIDs.Value = hidCKIDs.Value.Trim(',');
                    hidCWIDs.Value = hidCWIDs.Value.Trim(',');
                    hidDWBH.Value = hidDWBH.Value.Trim(',');
                }
            }
        }

        protected void btnSave_Click(object sender, ImageClickEventArgs e)
        {
            ZL_YWLSH ywItem = new ZL_YWLSH();
            bool isAdd = true;
            bool result;
            using (JCCDBLL jBLL = new JCCDBLL())
            {
                JCCD entity = new JCCD();
                if (DJID != Guid.Empty)
                    entity = jBLL.GetModel(t => t.ID == DJID);
                else
                {
                    entity.ID = Guid.NewGuid();
                    entity.YWLXBH = EnumHelper.GetDescriptionByName(typeof(EnumYWDJLX), EnumYWDJLX.BZX_CC);
                    entity.BH = GenYWLSH.GenBH(entity.YWLXBH, out ywItem, out isAdd);
                    entity.ZT = 0;
                }
                //entity.LRRLX = 0; //录入人类型：0,部门；1,供应商
                entity.BMID = Guid.Parse(this.ddlDepartments.SelectedValue);
                entity.RYID = Guid.Parse(hidStaff.Value);
                entity.CGYID = this.currentUser.UserID;
                entity.RQ = DateTime.Now;
                entity.JCCYYBH = this.ddlReason.SelectedValue;
                entity.GLDJID = this.txtGLDJ.Text.Trim();
                entity.KHID = Guid.Parse(hidKHID.Value);
                entity.BZ = txtBZ.Text.Trim();
                //明细表
                List<JCCDMXB> delMXB = entity.JCCDMXB.ToList();//旧的待删明细表
                string[] ids = hidWLID.Value.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
                string[] counts = hidCounts.Value.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
                string[] ckids = hidCKIDs.Value.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
                string[] cwids = hidCWIDs.Value.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
                using (KCBLL kBLL = new KCBLL())
                {
                    for (int i = 0; i < ids.Count(); i++)
                    {
                        JCCDMXB item = new JCCDMXB();
                        item.ID = Guid.NewGuid();
                        item.WPID = Guid.Parse(ids[i]);
                        item.WPLX = EnumHelper.GetDescriptionByName(typeof(EnumKC_LX), EnumKC_LX.BZX);
                        item.YWLXBH = entity.YWLXBH;
                        item.SL = -Convert.ToDouble(counts[i]);
                        item.CKID = Guid.Parse(ckids[i]);
                        item.CWID = Guid.Parse(cwids[i]);
                        item.YKCSL = kBLL.GetYKCSL(item.WPID, item.WPLX);
                        item.JCSJ = DateTime.Now;
                        entity.JCCDMXB.Add(item);
                    }
                }
                if (DJID != Guid.Empty)
                    result = jBLL.Update<JCCDMXB>(entity, delMXB);
                else
                    result = jBLL.Insert(entity);
            }
            if (result)
            {
                btnSave.Visible = false;
                labResult.Text = "保存成功";
                using (ZL_YWLSHBLL zBLL = new ZL_YWLSHBLL())
                {
                    if (ywItem.ID != Guid.Empty)
                    {
                        if (isAdd)
                            zBLL.Insert(ywItem);
                        else
                            zBLL.Update(ywItem);
                    }
                }
            }
            else
                labResult.Text = "保存失败";
            BindData();
        }
    }
}